package com.gdth.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Vector;

import com.gdth.bank.BankApplication;
import com.gdth.resource.Constant;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;

public class ToolUtil {
	
	public static String saveFolder = "/.Arecord";
	
	public static String SpalshBackGround = "spalshbackground.jpg";
	public static String LoginBackGround = "loginbackground.jpg";
	
	private static String shareName = "mediorecorder_info";
	private static String recorderType = "mediorecorder_type";
	
	
	/**
	 * 获取附件保存文件夹
	 * @return
	 */
	public static String getFileFullPath(String path){
		getSavePath();
		String fullPath = getSDPath().getAbsolutePath() + path;
		return fullPath;
	}
	
	public static String getImagePath(String path) {
		getSavePath1();
		return getSDPath().getAbsolutePath() + saveFolder + Constant.bankImagePath + path;
	}
	
	private static File getSavePath1(){
		File file = getSDPath();
        File saveFile = new File(file, saveFolder + Constant.bankImagePath);
        if (!saveFile.exists())
        	saveFile.mkdirs();
        return saveFile;
	}
	
	
	
	/**
	 * 获取文件保存路径
	 * @param fileName
	 * @return
	 */
	public static String getFilePath(String fileName){
		String path = saveFolder + "/" + fileName;
		return path;
	}
	
	public static String getDateFormat(){
		Date mDate = new Date(System.currentTimeMillis());
		return new SimpleDateFormat("yyyyMMddHHmmss", Locale.getDefault()).format(mDate);
	}
	
	public static File createFile(String name){
		File file = new File(createFolder(), name);
		
		try {    
			file.createNewFile();
        } catch (IOException e) {    
            e.printStackTrace();
            
            return null;
        }
		
		return file;
	}
	
	public static File createFolder(){
		File recordPath = getSavePath();    
        if (!recordPath.exists()) {    
            recordPath.mkdirs();       
        }
        
        return recordPath;
	}
	
	public static Vector<String> GetVideoFileName() {
        Vector<String> vecFile = new Vector<String>();
		
        File file = getSavePath();
        
        File[] subFile = file.listFiles();
        if (subFile != null){
	        for (int iFileLength = 0; iFileLength < subFile.length; iFileLength++) {
	            if (!subFile[iFileLength].isDirectory()) {
	                String filename = subFile[iFileLength].getName();
	//                if (filename.trim().toLowerCase().endsWith(".mp4")) {
	                    vecFile.add(filename);
	//                }
	            }
	        }
        }
        return vecFile;
	}
	
	public static void saveType(Context mContext, String type){
		SharedPreferences preference = mContext.getSharedPreferences(shareName, Context.MODE_PRIVATE);
		
        SharedPreferences.Editor editor = preference.edit();
      
        editor.putString(recorderType, type);

        editor.commit();
	}
	
	public static void deleteFile(String name) {
		String path = getFilePath(name);
		File file = new File(path);
		
		deleteFile(file);
	}
	
	public static void deleteFile(File file) {
		if (file.exists()) {
			if (file.isFile()) {
					file.delete();
			} else if (file.isDirectory()) {
				File files[] = file.listFiles();
				for (int i = 0; i < files.length; i++) {
					deleteFile(files[i]);
				}
			}
	
			file.delete();
		}
	}
	
	private static File getSavePath(){
		File file = getSDPath();
        File saveFile = new File(file, saveFolder);
        if (!saveFile.exists())
        	saveFile.mkdirs();
        return saveFile;
	}
	
	
	private static File getSDPath(){
		File filePath = null;
		if (checkSDCard()) { 
			filePath = Environment.getExternalStorageDirectory(); 
		}
		else{ 
			filePath = BankApplication.mInstance.getBaseContext().getCacheDir(); 
		}
		return filePath;
	}
	
	public static boolean checkSDCard(){
		//判断sd卡是否存在
		boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
		 
//		Android中判断SD卡是否存在，并且可以进行写操作，可以使用如下代码
//		if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
//		}
		
		return sdCardExist;
	}
	
	/**
	 * 复制文件
	 * @param fromPath
	 * @param toPath
	 * @param reWrite
	 */
	public static void copyFile(String fromPath, String toPath, boolean reWrite) {
		File fromFile = new File(fromPath);
		File toFile = new File(toPath);
		
		copyFile(fromFile, toFile, reWrite);
	}
		
	/**
	 * 
	 * @param fromFile 被复制的文件
	 * @param toFile 复制的目录文件
	 * @param rewrite 是否重新创建文件
	 * 
	 * <p>文件的复制操作方法
	 */
	public static void copyFile(File fromFile, File toFile,Boolean rewrite) {
			
		if(!fromFile.exists()) {
			return;
		}
		
		if(!fromFile.isFile()) {
			return;
		}
		if(!fromFile.canRead()) {
			return;
		}
		if(!toFile.getParentFile().exists()) {
			toFile.getParentFile().mkdirs();
		}
		if(toFile.exists() && rewrite) {
			toFile.delete();
		}
		
		try {
			FileInputStream fosfrom = new FileInputStream(fromFile);
			FileOutputStream fosto = new FileOutputStream(toFile);
			
			byte[] bt = new byte[1024];
			int c;
			while((c=fosfrom.read(bt)) > 0){
				fosto.write(bt,0,c);
			}
			
			//关闭输入、输出流
			fosfrom.close();
			fosto.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
